setwd("d:/PORES Dropbox/Stephen Pettigrew/ranked-choice-voting/replication files/")

require(tidyverse)
source("code/geography-colors.R")

options(scipen = 999)

raw <- readRDS("final-data/all-errors-and-votes.RDS")


errors <- raw$errors %>%
  filter(geography != "ME") %>%
  filter(bubble.pattern != "") %>%
  select(geography, date, office.id, rcv, r1.vote, r1.valid.vote, vote.for, valid.vote)



rej.pct.office.r1 <- errors %>%
  mutate(rej.vote = !r1.valid.vote) %>%
  group_by(geography, date, rcv, office.id) %>%
  summarize(rej = mean(rej.vote),
            n = n()) %>%
  ungroup() %>%
  mutate(id = paste(geography, format(ymd(date), "%b. '%y"), sep = "\n"),
         type = ifelse(rcv, "RCV-first round","Non-ranked\nchoice race")) %>%
  rename(pct = rej)



rej.pct.office.final <- errors %>%
  mutate(rej.vote = !valid.vote,
         rej.vote = ifelse(vote.for == "exhausted", F, rej.vote)) %>%
  #filter(vote.for != "exhausted") %>%
  group_by(geography, date, rcv, office.id) %>%
  summarize(rej = mean(rej.vote),
            n = n()) %>%
  ungroup() %>%
  filter(!is.na(rej)) %>%
  mutate(id = paste(geography, format(ymd(date), "%b. '%y"), sep = "\n"),
         type = "RCV-final round") %>%
  rename(pct = rej)

dd <- rej.pct.office.r1 %>%
  bind_rows(rej.pct.office.final) %>%
  mutate(type = factor(type, levels = c("Non-ranked\nchoice race","RCV-first round","RCV-final round")))


dd.geo <- dd %>%
  group_by(geography,date, id, type) %>%
  summarize(se = sd(pct) / sqrt(n()),
            pct = mean(pct),
            lower = pct + qnorm(.025) * se,
            upper = pct + qnorm(.975) * se) %>%
  mutate(geography = factor(geography, levels = names(colors))) %>%
  ungroup()

### Figure: Full Graph -------------

ggplot() + 
  
  # dots for each election
  geom_point(mapping = aes(x = id, y = pct, shape = type),
             data = dd,
             position = position_dodge(width = .5),
             alpha = .5) + 
  
  # colored bars for each state avg
  geom_errorbar(mapping = aes(ymin = pct, 
                              ymax = pct, 
                              x = id,
                              width = .6, # horiz width
                              color = type), 
                data = dd.geo,
                linewidth = .5, # vert thickness
                position = position_dodge(width = .5)) + 
  geom_errorbar(mapping = aes(ymin = lower,
                              ymax = upper,
                              x = id,
                              width = 0,
                              color = type),
                data = dd.geo,
                position = position_dodge(width = .5),
                size = .6) + 
  scale_y_continuous("Percent of votes rejected due to error",
                     labels = scales::percent_format(.1),
                     breaks = seq(0,1,.005)) + 
  xlab("") + 
  theme_bw() + 
  scale_color_manual("", values = c("Non-ranked\nchoice race"="red",
                                                         "RCV-first round"="blue",
                                                         "RCV-final round"="orange")) + 
  scale_shape_discrete("")
